import time

class timer(object):
    def __init__(self, name):
        self.name = name
    
    def __enter__(self):
        self.t0 = time.time()
    
    def __exit__(self, t, v, tb):
        dur = time.time() - self.t0
        print '%s uses %.2fs' % (self.name, dur)
        
        
import gzip,bz2,cPickle

data = dict()
for i in xrange(16000):
    data[i] = range(100)

with timer('cpickle-w'):
    cPickle.dump(data, open('d.cp','wb'))
with timer('cpickle-r'):
    cPickle.load(open('d.cp','rb'))
    
for cl in xrange(1,10):
    with timer('cp-gzip-w%d'%cl):
        cPickle.dump(data, gzip.open('d.cp.gz-%d'%cl,'wb',compresslevel = cl))
    #with timer('cp-gzip-r%d'%cl): # way too slow!
    #    cPickle.load(gzip.GzipFile('d.cp.gz-%d'%cl,'rb'))
    #with timer('cp-gzip-memr%d'%cl):
    #    cpdata = gzip.open('d.cp.gz-%d'%cl,'rb').read()
    #    cPickle.loads(cpdata)
    with timer('cp-gzip-filer%d'%cl):
        f = open('d.cp.gz-%d'%cl,'rb')
        cpdata = gzip.GzipFile(fileobj=f).read()
        cPickle.loads(cpdata)    
    
'''
for cl in xrange(1,10):
    with timer('cpickle-bz2-w%d'%cl):
        cPickle.dump(data, bz2.BZ2File('d.cp.bz2-%d'%cl,'wb',compresslevel = cl))
    with timer('cpickle-bz2-r%d'%cl):
        cPickle.load(bz2.BZ2File('d.cp.bz2-%d'%cl,'rb'))
'''    
    